package com.sghd.football.module.game.service.config;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.football.module.data.entity.ConfigRoom;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * \* Created with 王创辉.
 * \* Date: 2018/5/21
 * \* Time: 12:16
 * \* @desc: 从数据库加载配置
 */
@Component
public class GameConfigDb extends AbstractTemplete {

    @Override
    public void read() {
        List<ConfigRoom> configs = querier.findAll(ConfigRoom.class);
        configs.forEach(config -> {
            com.sghd.football.module.game.service.config.model.ConfigRoom roomConfig = new com.sghd.football.module.game.service.config.model.ConfigRoom();
            roomConfig.setId(config.getId());
            roomConfig.setGameId(config.getGameId());
            roomConfig.setRoomName(config.getRoomName());
            roomConfig.setFreeCount(config.getFreeCount());
            roomConfig.setRebate(config.getRebate());
            String rates = config.getRates();
            Integer[] rateArray = JsonUtils.string2Array(rates, Integer.class);
            roomConfig.setRates(rateArray);
            String betArea = config.getBetArea();
            Integer[] betAreaArray = JsonUtils.string2Array(betArea, Integer.class);
            roomConfig.setBetArea(betAreaArray);
            String limitMoney = config.getLimitMoney();
            Long[] limitMoneyArray = JsonUtils.string2Array(limitMoney, Long.class);
            roomConfig.setLimitMoney(limitMoneyArray);
            String count2Rate = config.getCount2Rate();
            Map<Integer, Map<Integer, Integer>> count2RateArray = JsonUtils.string2GenericObject(count2Rate, new TypeReference<Map<Integer, Map<Integer, Integer>>>() {
            });
            roomConfig.setCount2Rate(count2RateArray);
            roomConfig.setPushInterval(config.getPushInterval());
            String rowColStr = config.getRowCol();
            Integer[] rowCol = JsonUtils.string2Array(rowColStr, Integer.class);
            roomConfig.setRowCol(rowCol);
            roomConfig.setTimeout(config.getTimeout());
            roomConfig.setPlayerLimit(config.getPlayerLimit());
            roomConfig.setRefund(config.getRefund());
            roomConfig.setRewardRate(config.getRewardRate());
            String countRateS = config.getCountRate();
            Float[] countrate = JsonUtils.string2Array(countRateS, Float.class);
            roomConfig.setCountRate(countrate);
            String rateFreeStr = config.getRatesFree();
            Integer[] ratesFree = JsonUtils.string2Array(rateFreeStr,Integer.class);
            roomConfig.setRatesFree(ratesFree);
            roomConfig.setNormalRandomSection(config.getNormalRandomSection());
            roomConfig.setNormalBei(config.getNormalBei());
            roomConfig.setNormalPeiRate(config.getNormalPeiRate());
            String rewardRangst = config.getRewardRang();
            Integer[] rewardRang = JsonUtils.string2Array(rewardRangst,Integer.class);
            roomConfig.setRewardRang(rewardRang);
            roomConfig.setDrawWaters(config.getDrawWaters());
            String refundCtrlS = config.getRefundCtrl();
            Map<Integer, int[]> refundCtrl = JsonUtils.string2GenericObject(refundCtrlS, new TypeReference<Map<Integer, int[]>>() {});
            roomConfig.setRefundCtrl(refundCtrl);
            roomConfig.setRoomId(config.getRoomId());
            this.configRooms.put(roomConfig.getId(), roomConfig);
        });
    }

    @Override
    public void reload() {
        super.init();
    }

}
